package com.ymttest.database.sharding.db.sqlwapper.shenqi;

import java.math.BigDecimal;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.apache.commons.collections.CollectionUtils;
import org.apache.ibatis.session.SqlSession;

import com.google.common.collect.Iterables;
import com.shop2cn.iapi.sqoptrade.parameter.utill.Utils;
import com.shop2cn.iapi.sqsynctrade.paremeter.mongo.Order;
import com.shop2cn.iapi.sqsynctrade.paremeter.mongo.SourceData;
import com.shop2cn.iapi.sqsynctrade.paremeter.mongo.details.Delivery;
import com.shop2cn.iapi.sqsynctrade.paremeter.mongo.details.Fund;
import com.shop2cn.iapi.sqsynctrade.paremeter.mongo.details.PickupStore;
import com.shop2cn.iapi.sqsynctrade.paremeter.mongo.details.PrepaidCardRefund;
import com.shop2cn.iapi.sqsynctrade.paremeter.mongo.details.Product;
import com.shop2cn.iapi.sqsynctrade.paremeter.mongo.details.ProductProxyPo;
import com.shop2cn.iapi.sqsynctrade.paremeter.mongo.details.ProductSubCode;
import com.shop2cn.iapi.sqsynctrade.paremeter.mongo.details.Refund;
import com.shop2cn.iapi.sqsynctrade.paremeter.mongo.details.StatusLog;
import com.shop2cn.iapi.sqsynctrade.paremeter.mongo.details.SubProduct;
import com.ymt.core.tool.Logger;
import com.ymttest.database.sharding.config.DbShardingSourceConfig;
import com.ymttest.database.sharding.db.ext.query.CommandExtQuery;
import com.ymttest.database.sharding.db.sharding.mapper.AutoReceiveOptDetailMapper;
import com.ymttest.database.sharding.db.sharding.mapper.AutoReceiveOptMapper;
import com.ymttest.database.sharding.db.sharding.mapper.DeliveryBizMapper;
import com.ymttest.database.sharding.db.sharding.mapper.ExtendReceiveMapper;
import com.ymttest.database.sharding.db.sharding.mapper.MainOrderMapper;
import com.ymttest.database.sharding.db.sharding.mapper.OrderBizLogMapper;
import com.ymttest.database.sharding.db.sharding.mapper.OrderBizMapper;
import com.ymttest.database.sharding.db.sharding.mapper.OrderCouponDetailMapper;
import com.ymttest.database.sharding.db.sharding.mapper.OrderDetailExtMapper;
import com.ymttest.database.sharding.db.sharding.mapper.OrderDetailMapper;
import com.ymttest.database.sharding.db.sharding.mapper.OrderDetailPromotionMapper;
import com.ymttest.database.sharding.db.sharding.mapper.OrderDetailSubCodeMapper;
import com.ymttest.database.sharding.db.sharding.mapper.OrderDetailSubMapper;
import com.ymttest.database.sharding.db.sharding.mapper.OrderExtMapper;
import com.ymttest.database.sharding.db.sharding.mapper.OrderMapper;
import com.ymttest.database.sharding.db.sharding.mapper.OrderStatusLogMapper;
import com.ymttest.database.sharding.db.sharding.mapper.OrderSyncCmdMapper;
import com.ymttest.database.sharding.db.sharding.mapper.OrderTravelerMapper;
import com.ymttest.database.sharding.db.sharding.mapper.OuterPromotionMapper;
import com.ymttest.database.sharding.db.sharding.mapper.PaymentDetailMapper;
import com.ymttest.database.sharding.db.sharding.mapper.PaymentMapper;
import com.ymttest.database.sharding.db.sharding.mapper.PickInStoreMapper;
import com.ymttest.database.sharding.db.sharding.mapper.RefundBillMapper;
import com.ymttest.database.sharding.db.sharding.mapper.RefundFundMapper;
import com.ymttest.database.sharding.db.sharding.mapper.ShenqiProxyChainMapper;
import com.ymttest.database.sharding.db.sharding.mapper.ShenqiSellerProfitMapper;
import com.ymttest.database.sharding.db.sharding.mapper.UserRefundFundDetailMapper;
import com.ymttest.database.sharding.db.sharding.model.AutoReceiveOptDetailExample;
import com.ymttest.database.sharding.db.sharding.model.AutoReceiveOptDetailPo;
import com.ymttest.database.sharding.db.sharding.model.AutoReceiveOptExample;
import com.ymttest.database.sharding.db.sharding.model.AutoReceiveOptPo;
import com.ymttest.database.sharding.db.sharding.model.DeliveryBizExample;
import com.ymttest.database.sharding.db.sharding.model.DeliveryBizPo;
import com.ymttest.database.sharding.db.sharding.model.ExtendReceiveExample;
import com.ymttest.database.sharding.db.sharding.model.ExtendReceivePo;
import com.ymttest.database.sharding.db.sharding.model.MainOrderExample;
import com.ymttest.database.sharding.db.sharding.model.MainOrderPo;
import com.ymttest.database.sharding.db.sharding.model.OrderBizExample;
import com.ymttest.database.sharding.db.sharding.model.OrderBizLogExample;
import com.ymttest.database.sharding.db.sharding.model.OrderBizLogPo;
import com.ymttest.database.sharding.db.sharding.model.OrderBizPo;
import com.ymttest.database.sharding.db.sharding.model.OrderCouponDetailExample;
import com.ymttest.database.sharding.db.sharding.model.OrderCouponDetailPo;
import com.ymttest.database.sharding.db.sharding.model.OrderDetailExample;
import com.ymttest.database.sharding.db.sharding.model.OrderDetailExtExample;
import com.ymttest.database.sharding.db.sharding.model.OrderDetailExtPo;
import com.ymttest.database.sharding.db.sharding.model.OrderDetailPo;
import com.ymttest.database.sharding.db.sharding.model.OrderDetailPromotionExample;
import com.ymttest.database.sharding.db.sharding.model.OrderDetailPromotionPo;
import com.ymttest.database.sharding.db.sharding.model.OrderDetailSubCodeExample;
import com.ymttest.database.sharding.db.sharding.model.OrderDetailSubCodePo;
import com.ymttest.database.sharding.db.sharding.model.OrderDetailSubExample;
import com.ymttest.database.sharding.db.sharding.model.OrderDetailSubPo;
import com.ymttest.database.sharding.db.sharding.model.OrderExample;
import com.ymttest.database.sharding.db.sharding.model.OrderExtExample;
import com.ymttest.database.sharding.db.sharding.model.OrderExtPo;
import com.ymttest.database.sharding.db.sharding.model.OrderPo;
import com.ymttest.database.sharding.db.sharding.model.OrderStatusLogExample;
import com.ymttest.database.sharding.db.sharding.model.OrderStatusLogPo;
import com.ymttest.database.sharding.db.sharding.model.OrderSyncCmdExample;
import com.ymttest.database.sharding.db.sharding.model.OrderSyncCmdPo;
import com.ymttest.database.sharding.db.sharding.model.OrderTravelerExample;
import com.ymttest.database.sharding.db.sharding.model.OrderTravelerPo;
import com.ymttest.database.sharding.db.sharding.model.OuterPromotionExample;
import com.ymttest.database.sharding.db.sharding.model.OuterPromotionPo;
import com.ymttest.database.sharding.db.sharding.model.PaymentDetailExample;
import com.ymttest.database.sharding.db.sharding.model.PaymentDetailPo;
import com.ymttest.database.sharding.db.sharding.model.PaymentExample;
import com.ymttest.database.sharding.db.sharding.model.PaymentPo;
import com.ymttest.database.sharding.db.sharding.model.PickInStoreExample;
import com.ymttest.database.sharding.db.sharding.model.PickInStorePo;
import com.ymttest.database.sharding.db.sharding.model.RefundBillExample;
import com.ymttest.database.sharding.db.sharding.model.RefundBillPo;
import com.ymttest.database.sharding.db.sharding.model.RefundFundExample;
import com.ymttest.database.sharding.db.sharding.model.RefundFundPo;
import com.ymttest.database.sharding.db.sharding.model.ShenqiProxyChainExample;
import com.ymttest.database.sharding.db.sharding.model.ShenqiProxyChainPo;
import com.ymttest.database.sharding.db.sharding.model.ShenqiSellerProfitExample;
import com.ymttest.database.sharding.db.sharding.model.ShenqiSellerProfitPo;
import com.ymttest.database.sharding.db.sharding.model.UserRefundFundDetailExample;
import com.ymttest.database.sharding.db.sharding.model.UserRefundFundDetailPo;
import com.ymttest.database.sharding.db.sharding.query.CommandShardingQuery;

/************************************************************************************
 * 新交易分库分表 查询wapper
 ************************************************************************************/
public class OrderShardingWapper {

	public static SqlSession shardingSqlSession;

	public void closeSession() {
		try {
			if (shardingSqlSession != null
					&& shardingSqlSession.getConnection() != null
					&& !shardingSqlSession.getConnection().isClosed()) {
				shardingSqlSession.close();
				shardingSqlSession = null;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	public void clearCache() {
		if(shardingSqlSession!=null) {
			shardingSqlSession.clearCache();
		}
	}

	/**
	 * 订单表(order)
	 */
	public OrderMapper orderMapper;

	public OrderExample orderExample;

	/**
	 * 订单业务来源表(order_biz)
	 */
	public OrderBizMapper orderBizMapper;

	public OrderBizExample orderBizExample;

	/**
	 * 主单表(main_order)
	 */
	public MainOrderMapper mainOrderMapper;

	public MainOrderExample mainOrderExample;

	/**
	 * 订单商品表(order_detail)
	 */
	public OrderDetailMapper orderDetailMapper;

	public OrderDetailExample orderDetailExample;

	/**
	 * 订单组合子商品表（order_detail_sub）
	 */
	public OrderDetailSubMapper orderDetailSubMapper;

	public OrderDetailSubExample orderDetailSubExample;

	/**
	 * 营销表（order_detail_promotion）
	 */
	public OrderDetailPromotionMapper orderDetailPromotionMapper;

	public OrderDetailPromotionExample orderDetailPromotionExample;

	/**
	 * 神器代理链(shenqi_proxy_chain)
	 */
	public ShenqiProxyChainMapper shenqiProxyChainMapper;

	public ShenqiProxyChainExample shenqiProxyChainExample;
	
	/**
	 * 神器买手利润(shenqi_seller_profit)
	 */
	public ShenqiSellerProfitMapper shenqiSellerProfitMapper;

	public ShenqiSellerProfitExample shenqiSellerProfitExample;
	
	/**
	 * 订单扩展信息(order_ext)
	 */
	public OrderExtMapper orderExtMapper;

	public OrderExtExample orderExtExample;
	
	/**
	 * 订单详情扩展信息(order_detail_ext)
	 */
	public OrderDetailExtMapper orderDetailExtMapper;

	public OrderDetailExtExample orderDetailExtExample;

	/**
	 * 支付表(payment)
	 */
	public PaymentMapper paymentMapper;

	public PaymentExample paymentExample;

	/**
	 * 订单支付详情表(payment_detail)
	 */
	public PaymentDetailMapper paymentDetailMapper;

	public PaymentDetailExample paymentDetailExample;

	/**
	 * 订单主状态操作日志表(order_status_log)
	 */
	public OrderStatusLogMapper orderStatusLogMapper;

	public OrderStatusLogExample orderStatusLogExample;

	/**
	 * .订单业务操作记录表(order_biz_log)
	 */
	public OrderBizLogMapper orderBizLogMapper;

	public OrderBizLogExample orderBizLogExample;

	/**
	 * 用户退款资金详情表（user_refund_fund_detail）
	 */
	public UserRefundFundDetailMapper userRefundFundDetailMapper;

	public UserRefundFundDetailExample userRefundFundDetailExample;

	/**
	 * 退款资金表（refund_fund）
	 */
	public RefundFundMapper refundFundMapper;

	public RefundFundExample refundFundExample;

	/**
	 * 退款业务表（refund_bill）
	 */
	public RefundBillMapper refundBillMapper;

	public RefundBillExample refundBillExample;
	/**
	 * 自动确认收货时间操作表（auto_receive_opt）
	 */
	public AutoReceiveOptMapper autoReceiveOptMapper;

	public AutoReceiveOptExample autoReceiveOptExample;

	/**
	 * 自动确认收货时间操作详情表（auto_receive_opt_detail）
	 */
	public AutoReceiveOptDetailMapper autoReceiveOptDetailMapper;

	public AutoReceiveOptDetailExample autoReceiveOptDetailExample;

	/**
	 * 延长收货表（extend_receive）
	 */
	public ExtendReceiveMapper extendReceiveMapper;

	public ExtendReceiveExample extendReceiveExample;

	/**
	 * order_sync_cmd(订单状态操作同步指令)
	 */
	public OrderSyncCmdMapper orderSyncCmdMapper;

	public OrderSyncCmdExample orderSyncCmdExample;
	
	/**
	 * delivery_biz 虚拟库存发货表
	 */
	public DeliveryBizMapper deliveryBizMapper;
	
	public DeliveryBizExample deliveryBizExample;
	
	/**
	 * outer_promotion 虚拟库存发货表
	 */
	public OuterPromotionMapper outerPromotionMapper;
	
	public OuterPromotionExample outerPromotionExample;
	
	/**
	 * order_traveler 出行人信息
	 */
	public OrderTravelerMapper orderTravelerMapper;
	
	public OrderTravelerExample orderTravelerExample;

	/**
	 * 同步服务查询mapper
	 */
	public CommandShardingQuery commandShardingMapper;
	
	/**
	 * 订单商品子码表(order_detail_sub_code)
	 */
	public OrderDetailSubCodeMapper orderDetailSubCodeMapper;

	public OrderDetailSubCodeExample orderDetailSubCodeExample;
	
	/**
	 * order_coupon_detail
	 */
	public OrderCouponDetailMapper orderCouponDetailMapper;
	
	public OrderCouponDetailExample orderCouponDetailExample;
	
	/**
	 * pick_in_store
	 */
	public PickInStoreMapper pickInStoreMapper;
	
	public PickInStoreExample pickInStoreExample;
	
	/**
	 * 激活死链接
	 */
	public void activeDeadCommunicationsLink(OrderShardingWapper wapper) {
		
		wapper.selectUserRefundFundDetailByBizno("4529488699");
		wapper.selectOrderbyOrderId(5427908699L);
		wapper.selectOrderDetailSubByDetailId(5460788699000L);
		wapper.selectOrderDetailPromotionByOrderDetailId(1105408699000L);
		wapper.selectRefundBillByBillNo("TK05205468699");
	}

	public OrderShardingWapper() {
		try {
			if (shardingSqlSession == null
					|| shardingSqlSession.getConnection() == null
					|| shardingSqlSession.getConnection().isClosed()) {
				shardingSqlSession = DbShardingSourceConfig.getInstance()
						.shardingSessionFactory().openSession();
			}
		} catch (Exception e) {
			Logger.comment("shardingSqlSession init failed:" + e.getMessage());
			e.printStackTrace();
		}

		orderMapper = shardingSqlSession.getMapper(OrderMapper.class);

		orderExample = new OrderExample();

		orderBizMapper = shardingSqlSession.getMapper(OrderBizMapper.class);

		orderBizExample = new OrderBizExample();

		mainOrderMapper = shardingSqlSession.getMapper(MainOrderMapper.class);

		mainOrderExample = new MainOrderExample();

		this.shenqiProxyChainMapper = shardingSqlSession
				.getMapper(ShenqiProxyChainMapper.class);
		this.shenqiProxyChainExample = new ShenqiProxyChainExample();
		
		this.shenqiSellerProfitMapper = shardingSqlSession
				.getMapper(ShenqiSellerProfitMapper.class);
		this.shenqiSellerProfitExample = new ShenqiSellerProfitExample();
		
		this.orderDetailSubCodeMapper = shardingSqlSession
				.getMapper(OrderDetailSubCodeMapper.class);
		this.orderDetailSubCodeExample = new OrderDetailSubCodeExample();

		orderDetailMapper = shardingSqlSession
				.getMapper(OrderDetailMapper.class);

		orderDetailExample = new OrderDetailExample();

		orderDetailSubMapper = shardingSqlSession
				.getMapper(OrderDetailSubMapper.class);

		orderDetailSubExample = new OrderDetailSubExample();

		orderDetailPromotionMapper = shardingSqlSession
				.getMapper(OrderDetailPromotionMapper.class);

		orderDetailPromotionExample = new OrderDetailPromotionExample();

		paymentMapper = shardingSqlSession.getMapper(PaymentMapper.class);

		paymentExample = new PaymentExample();

		paymentDetailMapper = shardingSqlSession
				.getMapper(PaymentDetailMapper.class);

		paymentDetailExample = new PaymentDetailExample();

		orderStatusLogMapper = shardingSqlSession
				.getMapper(OrderStatusLogMapper.class);

		orderStatusLogExample = new OrderStatusLogExample();

		orderBizLogMapper = shardingSqlSession
				.getMapper(OrderBizLogMapper.class);

		orderBizLogExample = new OrderBizLogExample();

		userRefundFundDetailMapper = shardingSqlSession
				.getMapper(UserRefundFundDetailMapper.class);

		userRefundFundDetailExample = new UserRefundFundDetailExample();

		refundFundMapper = shardingSqlSession.getMapper(RefundFundMapper.class);

		refundFundExample = new RefundFundExample();

		refundBillMapper = shardingSqlSession.getMapper(RefundBillMapper.class);

		refundBillExample = new RefundBillExample();

		autoReceiveOptMapper = shardingSqlSession
				.getMapper(AutoReceiveOptMapper.class);

		autoReceiveOptExample = new AutoReceiveOptExample();

		autoReceiveOptDetailMapper = shardingSqlSession
				.getMapper(AutoReceiveOptDetailMapper.class);

		autoReceiveOptDetailExample = new AutoReceiveOptDetailExample();

		extendReceiveMapper = shardingSqlSession
				.getMapper(ExtendReceiveMapper.class);

		extendReceiveExample = new ExtendReceiveExample();

		orderSyncCmdMapper = shardingSqlSession
				.getMapper(OrderSyncCmdMapper.class);

		orderSyncCmdExample = new OrderSyncCmdExample();

		commandShardingMapper = shardingSqlSession
				.getMapper(CommandShardingQuery.class);

		this.deliveryBizMapper = shardingSqlSession
				.getMapper(DeliveryBizMapper.class);
		
		this.deliveryBizExample = new DeliveryBizExample();
		
		this.orderExtMapper = shardingSqlSession
				.getMapper(OrderExtMapper.class);
		this.orderExtExample = new OrderExtExample();
		
		this.orderDetailMapper = shardingSqlSession
				.getMapper(OrderDetailMapper.class);
		this.orderDetailExtExample = new OrderDetailExtExample();
		
		this.outerPromotionMapper = shardingSqlSession
				.getMapper(OuterPromotionMapper.class);
		this.outerPromotionExample = new OuterPromotionExample();
		
		this.orderTravelerMapper = shardingSqlSession
				.getMapper(OrderTravelerMapper.class);
		this.orderTravelerExample = new OrderTravelerExample();
		
		this.orderCouponDetailMapper = shardingSqlSession
				.getMapper(OrderCouponDetailMapper.class);
		this.orderCouponDetailExample = new OrderCouponDetailExample();
		
		this.pickInStoreMapper = shardingSqlSession
				.getMapper(PickInStoreMapper.class);
		this.pickInStoreExample = new PickInStoreExample();
	}

	/**
	 * 根据主订单号查询 主单表(main_order)
	 * 
	 * @param mainOrderId
	 * @return
	 */
	public MainOrderPo selectMianOrderbyId(long mainOrderId) {

		mainOrderExample.clear();

		mainOrderExample.createCriteria().andMainOrderIdEqualTo(mainOrderId);

		List<MainOrderPo> res = mainOrderMapper
				.selectByExample(mainOrderExample);

		return Iterables.getFirst(res, null);

	}

	/**
	 * 根据用户编号查询 订单表(order)
	 * 
	 * @param orderId
	 * @return
	 */
	public List<OrderPo> selectOrderbyUserId(long userId) {

		orderExample.clear();

		orderExample.createCriteria().andUserIdEqualTo(userId);

		List<OrderPo> orderPoList = orderMapper.selectByExample(orderExample);

		return orderPoList;

	}

	/**
	 * 根据订单号查询 订单表(order)
	 * 
	 * @param orderId
	 * @return
	 */
	public OrderPo selectOrderbyOrderId(long orderId) {

		clearCache();

		return orderMapper.selectByPrimaryKey(orderId);

	}
	
	/**
	 * 根据订单号和订单类型查询 订单表(order)
	 * 
	 * @param orderId
	 * @param orderType
	 * @return
	 */
	public OrderPo selectOrderbyOrderIdAndOrderType(long orderId, int orderType) {

		orderExample.clear();

		orderExample.createCriteria().andOrderIdEqualTo(orderId).andOrderTypeEqualTo(orderType);

		List<OrderPo> orderPoList = orderMapper.selectByExample(orderExample);
		
		if (orderPoList.size() == 0)
			return null;
		else
			return orderPoList.get(0);
	}
	
	/**
	 * 根据linkOrderId查询 订单表(order)
	 * 
	 * @param linkOrderId
	 * @return
	 */
	public OrderPo selectOrderbyLinkPayanliOrderId(long linkOrderId) {

		orderExample.clear();

		orderExample.createCriteria().andLinkPayanliOrderIdEqualTo(linkOrderId);

		List<OrderPo> orderPoList = orderMapper.selectByExample(orderExample);
		
		if (orderPoList.size() == 0)
			return null;
		else
			return orderPoList.get(0);
	}

	/**
	 * 根据订单号查询 神器代理链(shenqi_proxy_chain)
	 * 
	 * @param orderId
	 * @return
	 */
	public List<ShenqiProxyChainPo> selectProxyChainbyOrderId(long orderId) {

		this.shenqiProxyChainExample.clear();

		shenqiProxyChainExample.createCriteria().andOrderIdEqualTo(orderId);

		List<ShenqiProxyChainPo> proxyChainPoList = this.shenqiProxyChainMapper
				.selectByExample(shenqiProxyChainExample);

		return proxyChainPoList;
	}

	/**
	 * 根据订单号和catalogId查询 神器代理链(shenqi_proxy_chain)
	 * 
	 * @param orderId
	 * @param catalogId
	 * @return
	 */
	public List<ShenqiProxyChainPo> selectProxyChainbyOrderIdAndCatalogId(
			long orderId, String catalogId) {

		this.shenqiProxyChainExample.clear();

		shenqiProxyChainExample.createCriteria().andOrderIdEqualTo(orderId)
				.andCatalogIdEqualTo(catalogId);

		List<ShenqiProxyChainPo> proxyChainPoList = this.shenqiProxyChainMapper
				.selectByExample(shenqiProxyChainExample);

		return proxyChainPoList;
	}
	
	/**
	 * 根据订单号和childId查询 神器代理链(shenqi_proxy_chain)
	 * 
	 * @param orderId
	 * @param childId
	 * @return
	 */
	public ShenqiProxyChainPo selectProxyChainbyOrderIdAndChildId(long orderId,
			long childId) {

		this.shenqiProxyChainExample.clear();

		shenqiProxyChainExample.createCriteria().andOrderIdEqualTo(orderId)
				.andChildSellerIdEqualTo(childId);

		List<ShenqiProxyChainPo> proxyChainPoList = this.shenqiProxyChainMapper
				.selectByExample(shenqiProxyChainExample);

		return Iterables.getFirst(proxyChainPoList, null);
	}
	
	/**
	 * 根据orderId、catalogId、sellerId查询 神器代理链(shenqi_proxy_chain)
	 * 
	 * @param orderId
	 * @param catalogId
	 * @param sellerId
	 * @return
	 */
	public ShenqiProxyChainPo selectProxyChainbyOrderIdAndCatalogIdAndSellerId(
			long orderId, String catalogId, Long sellerId) {

		this.shenqiProxyChainExample.clear();

		shenqiProxyChainExample.createCriteria().andOrderIdEqualTo(orderId)
				.andCatalogIdEqualTo(catalogId).andSellerIdEqualTo(sellerId);

		List<ShenqiProxyChainPo> proxyChainPoList = this.shenqiProxyChainMapper
				.selectByExample(shenqiProxyChainExample);

		return Iterables.getFirst(proxyChainPoList, null);
	}
	
	/**
	 * 根据订单号查询  神器买手利润(shenqi_seller_profit)
	 * 
	 * @param orderId
	 * @return
	 */
	public List<ShenqiSellerProfitPo> selectSellerProfitbyOrderId(long orderId) {

		this.shenqiSellerProfitExample.clear();

		shenqiSellerProfitExample.createCriteria().andOrderIdEqualTo(orderId);

		List<ShenqiSellerProfitPo> sellerProfitPoList = this.shenqiSellerProfitMapper
				.selectByExample(this.shenqiSellerProfitExample);

		return sellerProfitPoList;
	}

	/**
	 * 根据订单号和买手编号查询  神器买手利润(shenqi_seller_profit)
	 * 
	 * @param orderId
	 * @param sellerId
	 * @return
	 */
	public ShenqiSellerProfitPo selectSellerProfitbyOrderIdAndSellerId(long orderId, long sellerId) {

		this.shenqiSellerProfitExample.clear();

		shenqiSellerProfitExample.createCriteria().andOrderIdEqualTo(orderId)
				.andSellerIdEqualTo(sellerId);

		List<ShenqiSellerProfitPo> sellerProfitPoList = this.shenqiSellerProfitMapper
				.selectByExample(this.shenqiSellerProfitExample);

		return sellerProfitPoList.get(0);
	}
	
	/**
	 * 根据订单号查询 插入订单业务来源表(order_biz)
	 * 
	 * @param orderId
	 * @return
	 */
	public OrderBizPo selectOrderBizbyOrderId(long orderId) {

		OrderBizPo orderBizPo = orderBizMapper.selectByPrimaryKey(orderId);

		return orderBizPo;

	}

	/**
	 * 根据主订单号查询 订单表(order)
	 * 
	 * @param mainOrderId
	 * @return
	 */
	public List<OrderPo> selectOrderbyMainOrderId(long mainOrderId) {

		orderExample.clear();

		orderExample.createCriteria().andMainOrderIdEqualTo(mainOrderId);

		List<OrderPo> orderPoList = orderMapper.selectByExample(orderExample);

		return orderPoList;
	}

	/**
	 * 根据userId查询所有待支付的hunter大礼包订单 订单表(order)
	 * 
	 * @param userId
	 * @return
	 */
	public List<OrderPo> selectHunterGiftBagOrderbyUserId(long userId) {

		orderExample.clear();

		orderExample.createCriteria().andUserIdandHunterIdEqualTo(userId);

		List<OrderPo> orderPoList = orderMapper.selectByExample(orderExample);

		return orderPoList;
	}

	/**
	 * 根据订单号查询 订单商品表(order_detail)
	 * 
	 * @param orderId
	 * @return
	 */
	public List<OrderDetailPo> selectOrderDetailbyOrderId(long orderId) {

		orderDetailExample.clear();

		orderDetailExample.createCriteria().andOrderIdEqualTo(orderId);

		List<OrderDetailPo> orderList = orderDetailMapper
				.selectByExample(orderDetailExample);

		return orderList;
	}
	
	/**
	 * 根据订单号和规格id查询 订单商品表(order_detail)
	 * 
	 * @param orderId
	 * @param catalogId
	 * @return
	 */
	public OrderDetailPo selectOrderDetailbyOrderIdAndCatalogId(long orderId, String catalogId) {

		orderDetailExample.clear();

		orderDetailExample.createCriteria().andOrderIdEqualTo(orderId).andCatalogIdEqualTo(catalogId);
		
		List<OrderDetailPo> orderList = orderDetailMapper
				.selectByExample(orderDetailExample);

		return orderList.get(0);
	}
	
	/**
	 * 根据订单号和货头规格id查询 订单商品表(order_detail)
	 * 
	 * @param orderId
	 * @param catalogId
	 * @return
	 */
	public OrderDetailPo selectOrderDetailbyOrderIdAndGoodsOwnerCatalogId(long orderId, String goodsOwnerCatalogId) {

		orderDetailExample.clear();

		orderDetailExample.createCriteria().andOrderIdEqualTo(orderId).andGoodsOwnerCatalogIdEqualTo(goodsOwnerCatalogId);
		
		List<OrderDetailPo> orderList = orderDetailMapper
				.selectByExample(orderDetailExample);

		return orderList.get(0);
	}

	/**
	 * 根据orderDetailId查询 订单组合子商品表（order_detail_sub）
	 * 
	 * @param orderDetailId
	 * @return
	 */
	public List<OrderDetailSubPo> selectOrderDetailSubById(long orderId) {

		orderDetailSubExample.clear();

		orderDetailSubExample.createCriteria().andOrderIdEqualTo(orderId);

		List<OrderDetailSubPo> orderList = orderDetailSubMapper
				.selectByExample(orderDetailSubExample);

		return orderList;

	}

	/**
	 * 根据orderDetailId查询 订单组合子商品表（order_detail_sub）
	 * 
	 * @param orderDetailId
	 * @return
	 */
	public List<OrderDetailSubPo> selectOrderDetailSubByDetailId(
			long orderDetailId) {

		orderDetailSubExample.clear();

		orderDetailSubExample.createCriteria().andOrderDetailIdEqualTo(
				orderDetailId);

		List<OrderDetailSubPo> orderList = orderDetailSubMapper
				.selectByExample(orderDetailSubExample);

		return orderList;

	}

	/**
	 * 根据订单号查询 营销表（order_detail_promotion）
	 * 
	 * @param orderId
	 * @return
	 */
	public List<OrderDetailPromotionPo> selectOrderDetailPromotionByOrderId(
			long orderId) {

		orderDetailPromotionExample.clear();

		orderDetailPromotionExample.createCriteria().andOrderIdEqualTo(orderId);

		List<OrderDetailPromotionPo> orderList = orderDetailPromotionMapper
				.selectByExample(orderDetailPromotionExample);

		return orderList;

	}

	/**
	 * 根据订单详情id查询 营销表（order_detail_promotion）
	 * 
	 * @param orderDetailId
	 * @return
	 */
	public OrderDetailPromotionPo selectOrderDetailPromotionByOrderDetailId(
			long orderDetailId) {

		orderDetailPromotionExample.clear();

		orderDetailPromotionExample.createCriteria().andOrderDetailIdEqualTo(
				orderDetailId);

		List<OrderDetailPromotionPo> res = orderDetailPromotionMapper
				.selectByExample(orderDetailPromotionExample);

		return Iterables.getFirst(res, null);
	}
	
	/**
	 * 根据订单号和订单详情id查询 营销表（order_detail_promotion）
	 * 
	 * @param orderDetailId
	 * @return
	 */
	public List<OrderDetailPromotionPo> selectOrderDetailPromotionByOrderDetailIdAndOrderId(
			long orderDetailId, long orderId) {

		orderDetailPromotionExample.clear();

		orderDetailPromotionExample.createCriteria().andOrderDetailIdEqualTo(
				orderDetailId).andOrderIdEqualTo(orderId);

		List<OrderDetailPromotionPo> res = orderDetailPromotionMapper
				.selectByExample(orderDetailPromotionExample);

		return res;
	}

	/**
	 * 根据payment_id查询 支付表(payment)
	 * 
	 * @param paymentId
	 * @return
	 */
	public PaymentPo selectPaymentById(long paymentId) {

		return paymentMapper.selectByPrimaryKey(paymentId);

	}

	/**
	 * 根据payment_id查询 订单支付详情表(payment_detail)
	 * 
	 * @param paymentId
	 * @return
	 */
	public List<PaymentDetailPo> selectPaymentDetailByPaymentId(long paymentId) {

		paymentDetailExample.clear();

		paymentDetailExample.createCriteria().andPaymentIdEqualTo(paymentId);

		List<PaymentDetailPo> res = paymentDetailMapper
				.selectByExample(paymentDetailExample);

		return res;

	}

	/**
	 * 根据orderId查询 订单支付详情表(payment_detail)
	 * 
	 * @param orderId
	 * @return
	 */
	public PaymentDetailPo selectPaymentDetailByOrderId(long orderId) {

		paymentDetailExample.clear();

		paymentDetailExample.createCriteria().andOrderIdEqualTo(orderId);

		List<PaymentDetailPo> res = paymentDetailMapper
				.selectByExample(paymentDetailExample);

		return Iterables.getFirst(res, null);

	}

	/**
	 * 根据orderId查询 订单主状态操作日志表(order_status_log)
	 * 
	 * @param orderId
	 * @return 订单最后一个操作数据记录
	 */
	public OrderStatusLogPo selectOrderStatusLogByOrderId(long orderId) {

		orderStatusLogExample.clear();

		orderStatusLogExample.createCriteria().andOrderIdEqualTo(orderId);

		orderStatusLogExample.setOrderByClause("id desc");

		List<OrderStatusLogPo> res = orderStatusLogMapper
				.selectByExample(orderStatusLogExample);

		return Iterables.getFirst(res, null);

	}
	
	/**
	 * 根据orderId查询 订单主状态操作日志表(order_status_log)
	 * 
	 * @param orderId
	 * @return
	 */
	public List<OrderStatusLogPo> selectOrderStatusesLogByOrderId(long orderId) {

		orderStatusLogExample.clear();

		orderStatusLogExample.createCriteria().andOrderIdEqualTo(orderId);

		orderStatusLogExample.setOrderByClause("id desc");

		List<OrderStatusLogPo> res = orderStatusLogMapper
				.selectByExample(orderStatusLogExample);

		return res;
	}

	/**
	 * 根据orderId查询 订单业务操作记录表(order_biz_log)
	 * 
	 * @param orderId
	 * @return 订单最后一个操作数据记录
	 */
	public OrderBizLogPo selectOrderBizLogByOrderId(long orderId) {

		orderBizLogExample.clear();

		orderBizLogExample.createCriteria().andOrderIdEqualTo(orderId);

		orderBizLogExample.setOrderByClause("id desc");

		List<OrderBizLogPo> res = orderBizLogMapper
				.selectByExample(orderBizLogExample);

		return Iterables.getFirst(res, null);

	}

	/**
	 * 根据paymentId查询 用户退款资金详情表（user_refund_fund_detail）
	 * 
	 * @param paymentId
	 * @return
	 */
	public UserRefundFundDetailPo selectUserRefundFundDetailByPaymentId(
			long paymentId) {

		userRefundFundDetailExample.clear();

		userRefundFundDetailExample.createCriteria().andPaymentIdEqualTo(
				paymentId);

		List<UserRefundFundDetailPo> res = userRefundFundDetailMapper
				.selectByExample(userRefundFundDetailExample);

		return Iterables.getFirst(res, null);

	}

	/**
	 * 根据paymentId查询 用户退款资金详情表（user_refund_fund_detail）
	 * 
	 * @param paymentId
	 * @return
	 */
	public List<UserRefundFundDetailPo> selectUserRefundFundDetailListByPaymentId(
			long paymentId) {

		userRefundFundDetailExample.clear();

		userRefundFundDetailExample.createCriteria().andPaymentIdEqualTo(
				paymentId);

		List<UserRefundFundDetailPo> res = userRefundFundDetailMapper
				.selectByExample(userRefundFundDetailExample);

		return res;

	}

	/**
	 * 根据paymentId查询 用户退款资金详情表（user_refund_fund_detail）
	 * 
	 * @param paymentId
	 * @return
	 */
	public UserRefundFundDetailPo selectUserRefundFundDetailByBizno(String bizno) {

		userRefundFundDetailExample.clear();

		userRefundFundDetailExample.createCriteria().andBizNoEqualTo(bizno);

		List<UserRefundFundDetailPo> res = userRefundFundDetailMapper
				.selectByExample(userRefundFundDetailExample);

		return Iterables.getFirst(res, null);

	}
	
	/**
	 * 根据bizno查询 神器用户退款资金详情表（user_refund_fund_detail）
	 * 
	 * @param paymentId
	 * @return
	 */
	public UserRefundFundDetailPo selectShenqiUserRefundFundDetailByBizno(String bizno) {

		userRefundFundDetailExample.clear();

		userRefundFundDetailExample.createCriteria().andBizNoEqualTo(bizno).andIsShenqiEqualTo(true);

		List<UserRefundFundDetailPo> res = userRefundFundDetailMapper
				.selectByExample(userRefundFundDetailExample);

		return Iterables.getFirst(res, null);

	}

	/**
	 * 根据orderId查询 退款资金表（refund_fund）
	 * 
	 * @param orderId
	 * @return
	 */
	public RefundFundPo selectRefundFundByOrderId(long orderId) {

		return refundFundMapper.selectByPrimaryKey(orderId);

	}

	/**
	 * 根据refundBillNo查询 退款业务表（refund_bill）
	 * 
	 * @param refundBillNo
	 * @return
	 */
	public RefundBillPo selectRefundBillByBillNo(String refundBillNo) {

		return refundBillMapper.selectByPrimaryKey(refundBillNo);

	}

	/**
	 * 根据orderId查询 退款业务表（refund_bill）
	 * 
	 * @param refundBillNo
	 * @return
	 */
	public RefundBillPo selectRefundBillByOrderId(long orderId) {

		refundBillExample.clear();
		refundBillExample.createCriteria().andOrderIdEqualTo(orderId);

		return Iterables.getFirst(
				refundBillMapper.selectByExample(refundBillExample), null);

	}

	/**
	 * 根据orderId查询 退款业务表（refund_bill）list
	 * 
	 * @param refundBillNo
	 * @return
	 */
	public List<RefundBillPo> selectRefundBillListByOrderId(long orderId) {

		refundBillExample.clear();
		refundBillExample.createCriteria().andOrderIdEqualTo(orderId);

		return refundBillMapper.selectByExample(refundBillExample);

	}

	/**
	 * 该订单下的所有退款单是否都成功 退款业务表（refund_bill）list
	 * 
	 * @param refundBillNo
	 * @return
	 */
	public boolean isOrderRefundSuccess(long orderId) {

		List<RefundBillPo> billList = this
				.selectRefundBillListByOrderId(orderId);
		boolean isSuccess = false;

		for (RefundBillPo billPo : billList) {

			if (billPo.getRefundStatus() == 1
					&& billPo.getRefundBillStatus() == 10)
				isSuccess = true;
			else {

				isSuccess = false;
				break;
			}
		}

		return isSuccess;
	}

	/**
	 * 根据orderId和catalogId查询 退款业务表（refund_bill）list
	 * 
	 * @param refundBillNo
	 * @param catalogId
	 * @return
	 */
	public RefundBillPo selectRefundBillByOrderIdAndCatalogId(long orderId,
			String catalogId) {

		refundBillExample.clear();
		refundBillExample.createCriteria().andOrderIdAndCatalogIdEqualTo(
				orderId, catalogId);
		
		refundBillExample.setOrderByClause("add_time DESC");

		return Iterables.getFirst(
				refundBillMapper.selectByExample(refundBillExample), null);

	}
	
	/**
	 * 根据orderId和catalogId查询 退款业务表（refund_bill）list
	 * 
	 * @param refundBillNo
	 * @param catalogId
	 * @return
	 */
	public List<RefundBillPo> selectRefundBillListByOrderIdAndCatalogId(long orderId,
			String catalogId) {

		refundBillExample.clear();
		refundBillExample.createCriteria().andOrderIdEqualTo(orderId)
				.andCatalogIdEqualTo(catalogId);
		
		refundBillExample.setOrderByClause("add_time DESC");

		return refundBillMapper.selectByExample(refundBillExample);
	}
	
	/**
	 * 根据orderId和catalogId查询神器 退款业务表（refund_bill）list
	 * 
	 * @param refundBillNo
	 * @param catalogId
	 * @return
	 */
	public RefundBillPo selectShenqiRefundBillByOrderIdAndCatalogId(long orderId,
			String catalogId) {

		refundBillExample.clear();
		refundBillExample.createCriteria().andOrderIdAndCatalogIdEqualToForShenqi(
				orderId, catalogId);
		
		refundBillExample.setOrderByClause("add_time DESC");

		return Iterables.getFirst(
				refundBillMapper.selectByExample(refundBillExample), null);

	}

	/**
	 * 根据orderId 自动确认收货时间操作表（auto_receive_opt）
	 * 
	 * @param orderId
	 * @return
	 */
	public AutoReceiveOptPo selectAutoReceiveOptByOrderId(long orderId) {

		return autoReceiveOptMapper.selectByPrimaryKey(orderId);

	}

	/**
	 * 根据orderId 自动确认收货时间操作详情表（auto_receive_opt_detail）
	 * 
	 * @param orderId
	 * @return
	 */
	public AutoReceiveOptDetailPo selectAutoReceiveOptDetailByOrderId(
			long orderId) {

		autoReceiveOptDetailExample.clear();

		autoReceiveOptDetailExample.createCriteria().andOrderIdEqualTo(orderId);

		autoReceiveOptDetailExample.setOrderByClause("id desc");

		return Iterables.getFirst(autoReceiveOptDetailMapper
				.selectByExample(autoReceiveOptDetailExample), null);

	}

	/**
	 * 根据orderId 查询延长收货表（extend_receive）
	 * 
	 * @param orderId
	 * @return
	 */
	public List<ExtendReceivePo> selectExtendReceiveByOrder(long orderId) {
		extendReceiveExample.clear();

		extendReceiveExample.createCriteria().andOrderIdEqualTo(orderId);

		extendReceiveExample.setOrderByClause("id desc");

		return extendReceiveMapper.selectByExample(extendReceiveExample);

	}

	/**
	 * 根据orderId order_sync_cmd(订单状态操作同步指令)
	 * 
	 * @param orderId
	 * @return
	 */
	public List<OrderSyncCmdPo> selectOrderSyncCmdByOrder(long orderId) {

		orderSyncCmdExample.clear();

		orderSyncCmdExample.createCriteria().andOrderIdEqualTo(orderId);

		orderSyncCmdExample.setOrderByClause("id desc");

		return orderSyncCmdMapper.selectByExample(orderSyncCmdExample);

	}
	
	/**
	 * 根据orderId查询 虚拟库存发货日志表(delivery_biz)
	 * 
	 * @param orderId
	 * @return 订单最后一个操作数据记录
	 */
	public DeliveryBizPo selectDeliveryBizByOrderId(long orderId) {

		this.deliveryBizExample.clear();

		deliveryBizExample.createCriteria().andOrderIdEqualTo(orderId);

		deliveryBizExample.setOrderByClause("add_time desc");

		List<DeliveryBizPo> res = this.deliveryBizMapper
				.selectByExample(deliveryBizExample);

		return Iterables.getFirst(res, null);

	}
	
	/**
	 * 根据订单号查询  订单扩展信息(order_ext)
	 * 
	 * @param orderId
	 * @return
	 */
	public OrderExtPo selectOrderExtbyOrderId(long orderId) {

		this.orderExtExample.clear();

		orderExtExample.createCriteria().andOrderIdEqualTo(orderId);

		List<OrderExtPo> orderExtPoList = this.orderExtMapper
				.selectByExample(this.orderExtExample);

		return Iterables.getFirst(orderExtPoList, null);
	}
	
	/**
	 * 根据订单号查询  订单详情扩展信息(order_detail_ext)
	 * 
	 * @param orderId
	 * @return
	 */
	public List<OrderDetailExtPo> selectOrderDetailExtbyOrderId(long orderId) {

		this.shenqiSellerProfitExample.clear();

		shenqiSellerProfitExample.createCriteria().andOrderIdEqualTo(orderId);

		List<OrderDetailExtPo> orderDetailExtPoList = this.orderDetailExtMapper
				.selectByExample(this.orderDetailExtExample);

		return orderDetailExtPoList;
	}
	
	/**
	 * 根据订单号查询 订单表(order)
	 * 
	 * @param orderId
	 * @return
	 */
	public OuterPromotionPo selectOuterPromotionbyOrderId(long orderId) {

		this.outerPromotionExample.clear();

		return outerPromotionMapper.selectByPrimaryKey(orderId);
	}
	
	/**
	 * 根据订单号查询 出行人信息
	 * 
	 * @param orderId
	 * @return
	 */
	public List<OrderTravelerPo> selectOrderTravelersbyOrderId(long orderId) {

		this.orderTravelerExample.clear();

		orderTravelerExample.createCriteria().andOrderIdEqualTo(orderId);
		
		return orderTravelerMapper.selectByExample(orderTravelerExample);
	}
	
	/**
	 * 根据订单号查询 商品子码信息
	 * 
	 * @param orderId
	 * @return
	 */
	public List<OrderDetailSubCodePo> selectOrderDetailSubCodebyOrderId(
			long orderId) {

		orderDetailSubCodeExample.clear();

		orderDetailSubCodeExample.createCriteria().andOrderIdEqualTo(orderId);

		return orderDetailSubCodeMapper
				.selectByExample(orderDetailSubCodeExample);
	}

	/**
	 * 根据订单号和规格id查询 商品子码信息
	 * 
	 * @param orderId
	 * @return
	 */
	public List<OrderDetailSubCodePo> selectOrderDetailSubCodebyOrderIdAndCatalogId(
			long orderId, String catalogId) {

		orderDetailSubCodeExample.clear();

		orderDetailSubCodeExample.createCriteria().andOrderIdEqualTo(orderId)
				.andCatalogIdEqualTo(catalogId);

		return orderDetailSubCodeMapper
				.selectByExample(orderDetailSubCodeExample);
	}
	
	/**
	 * 根据订单号、规格id和商品子码查询 商品子码信息
	 * 
	 * @param orderId
	 * @return
	 */
	public OrderDetailSubCodePo selectOrderDetailSubCodebyOrderIdAndCatalogIdAndSubCode(
			long orderId, String catalogId, String subCode) {

		orderDetailSubCodeExample.clear();

		orderDetailSubCodeExample.createCriteria().andOrderIdEqualTo(orderId)
				.andCatalogIdEqualTo(catalogId).andSubCodeEqualTo(subCode);

		return orderDetailSubCodeMapper
				.selectByExample(orderDetailSubCodeExample).get(0);
	}
	
	/**
	 * 根据订单号查询优惠券信息
	 * 
	 * @param orderId
	 * @return
	 */
	public List<OrderCouponDetailPo> selectOrderCouponDetailbyOrderId(long orderId) {

		this.orderCouponDetailExample.clear();

		orderCouponDetailExample.createCriteria().andOrderIdEqualTo(orderId);
		
		return orderCouponDetailMapper.selectByExample(orderCouponDetailExample);
	}
	
	/**
	 * 根据订单号查询自提信息
	 * 
	 * @param orderId
	 * @return
	 */
	public List<PickInStorePo> selectPickInStorebyOrderId(long orderId) {

		this.pickInStoreExample.clear();

		pickInStoreExample.createCriteria().andOrderIdEqualTo(orderId);
		
		return pickInStoreMapper.selectByExample(pickInStoreExample);
	}

	/**
	 * 同步服务查询
	 * 
	 * @param orderId
	 * @return
	 */
	public Order selectCommandByOrder(long orderId, long userId, Boolean isShenqi) {

		CommandExtQuery commandExtQuery = new OrderWapper().commandExtMapper;
		boolean isNewOrderExt = false;

		SourceData sourceData = new SourceData()
				.buildOrder(
						commandShardingMapper.getOrder(orderId),
						commandShardingMapper.getOrderPayment(orderId, userId),
						isShenqi ? commandShardingMapper.getShenqiProduct(orderId)
								: commandShardingMapper.getProduct(orderId),
						isNewOrderExt ? commandShardingMapper
								.getOrderExt(orderId) : commandExtQuery
								.getOrder(orderId),
						isNewOrderExt ? commandShardingMapper
								.getProductExt(orderId) : commandExtQuery
								.getProduct(orderId))
				.buildStatusLog(commandShardingMapper.getStatusLog(orderId))
				.buildDelivery(commandShardingMapper.getFreeze(orderId),
						commandShardingMapper.getFreezeBizType(orderId),
						// 查询物流单号
						new OrderWapper().selectDeliveryCodeByOrderId(orderId))
				.buildRefund(
						isShenqi ? commandShardingMapper
								.getShenqiRefund(orderId)
								: commandShardingMapper.getRefund(orderId))
				.buildSubProducts(commandShardingMapper.getSubProduct(orderId))
				.buildPrepaidCard(
						commandShardingMapper.getPrepaidCard(orderId, userId),
						commandShardingMapper.getPrepaidCardRefund(orderId))
				.buildThirdpartyRefundStatus(
						commandShardingMapper
								.getThirdpartyRefundStatus(orderId))
				.buildProductProxies(
						commandShardingMapper.getProductProxies(orderId))
				.buildOrderDetailSubCodes(
						commandShardingMapper.getOrderDetailSubCode(orderId))
				.buildPickup(commandShardingMapper.getPickUp(orderId));

		Order order = null;

		Map<String, Object> orderDetail = sourceData.getOrderDetail();

		long mainOrderId = (Long) orderDetail.get("mainOrderId");
		MainOrderPo actualMainOrder = this.selectMianOrderbyId(mainOrderId);
		BigDecimal payDeductAmount = actualMainOrder.getPayDeductAmount() == null ? BigDecimal.ZERO
				: actualMainOrder.getPayDeductAmount();
		
		Boolean isFullPaidOrder = (null != orderDetail.get("earnestPaymentId"))
				&& (null != orderDetail.get("paidTime"));

		if (isFullPaidOrder)
			orderDetail.put("earnestThirdpartyRefundStatus", 3);

		if (!CollectionUtils.isEmpty(sourceData.getThirdpartyRefundStatus())) {

			List<Map<String, Object>> fullThirdpartyRefundStatusMap = sourceData
					.getThirdpartyRefundStatus().stream()
					.filter(status -> (Integer) status.get("bizType") == 2)
					.collect(Collectors.toList());

			if (!CollectionUtils.isEmpty(fullThirdpartyRefundStatusMap)) {

				switch ((int) sourceData.getThirdpartyRefundStatus().get(0)
						.get("thirdpartyRefundStatus")) {
				case 3:
					orderDetail.put("fullThirdpartyRefundStatus", 3);
					break;
				default:
					orderDetail.put(
							"fullThirdpartyRefundStatus",
							sourceData.getThirdpartyRefundStatus().get(0)
									.get("thirdpartyRefundStatus"));
					break;
				}
			}
		}

		if (null == orderDetail.get("waitInStock"))
			orderDetail.put("waitInStock", false);

		if (null == orderDetail.get("bookingPromotionAmount"))
			orderDetail.put("bookingPromotionAmount", BigDecimal.ZERO);
		
		if (null == orderDetail.get("earnestAmount"))
			orderDetail.put("earnestAmount", BigDecimal.ZERO);

		if (null != orderDetail.get("paymentEndTime")) {

			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String timeStr = orderDetail.get("paymentEndTime").toString();
			Date dt = null;
			
			try {
				dt = sdf.parse(timeStr);
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			Calendar rightNow = Calendar.getInstance();
			rightNow.setTime(dt);
			rightNow.add(Calendar.DAY_OF_YEAR, 10);
			orderDetail.put("deliverEndTime", sdf.format(rightNow.getTime()));
		}

		orderDetail.put("payDeductAmount", payDeductAmount);

		Logger.info("sourceData.getOrderDetail():" + orderDetail);

		Logger.info("sourceData.getDelivery():" + sourceData.getDelivery());

		Logger.info("sourceData.getDeliveryCodes():"
				+ sourceData.getDeliveryCodes());

		try {

			order = (Order) Utils.mapToObject(orderDetail, Order.class);

		} catch (Exception e) {

			e.printStackTrace();
		}

		order.setAutoConfirm(false);
		order.setIsFullPaidOrder(isFullPaidOrder);

		// 解析订单 子状态里的 是否为自动确认收货
		if (orderDetail.get("autoConfirm") != null) {

			if ((Integer) orderDetail.get("autoConfirm") >= 10000
					&& (Integer) orderDetail.get("autoConfirm") < 20000) {
				order.setAutoConfirm(true);
			}

		}

		Map<String, Object> mapFund = new HashMap<String, Object>();

		orderDetail.forEach((k, v) -> {
			if (k.contains("fund_")) {
				mapFund.put(k.replace("fund_", ""), v);
			}
		});

		if (null == orderDetail.get("earnestPaidAmount"))
			mapFund.put("earnestPaidAmount", BigDecimal.ZERO);
		else
			mapFund.put("earnestPaidAmount",
					orderDetail.get("earnestPaidAmount"));

		Fund fund = null;

		try {
			fund = (Fund) Utils.mapToObject(mapFund, Fund.class);
			
			//fund.setPaidCash(fund.getPaidCash().subtract(fund.getPrepaidCard()));
		} catch (Exception e) {

			e.printStackTrace();
		}

		order.setFund(fund);

		// products
		List<Product> proList = new ArrayList<Product>();

		sourceData.getProducts().forEach(item -> {

			try {

				if (null == item.get("earnestAmount"))
					item.put("earnestAmount", BigDecimal.ZERO);

				if (null == item.get("bookingPromotionAmount"))
					item.put("bookingPromotionAmount", BigDecimal.ZERO);
				
				if (null == item.get("vStockCount"))
					item.put("vStockCount", 0);

				if (null == item.get("waitInStock"))
					item.put("waitInStock", false);

				if (null == item.get("primeCostPrice"))
					item.put("primeCostPrice", BigDecimal.ZERO);

				proList.add((Product) Utils.mapToObject(item, Product.class));
			} catch (Exception e) {

				e.printStackTrace();
			}
		});

		int type = order.getDeliveryType();

		proList.forEach(p -> p.setDeliveryType(type));
		
//		List<Product> newProList = proList.stream().collect(
//				Collectors.collectingAndThen(Collectors
//						.toCollection(() -> new TreeSet<>(Comparator
//								.comparing(Product::getCataId))),
//						ArrayList<Product>::new));

		order.setProducts(proList);

		// statusLog
		if (!CollectionUtils.isEmpty(sourceData.getStatusLogs())) {

			List<StatusLog> statusLogList = new ArrayList<StatusLog>();

			sourceData.getStatusLogs().forEach(
					item -> {

						try {
							statusLogList.add((StatusLog) Utils.mapToObject(
									item, StatusLog.class));
						} catch (Exception e) {

							e.printStackTrace();
						}
					}
			);

			order.setStatusLog(statusLogList);
		}

		Delivery delivery = null;

		// Delivery
		try {
			delivery = (Delivery) Utils.mapToObject(sourceData.getDelivery(),
					Delivery.class);

			if (delivery != null) {

				delivery.setBillCode(sourceData.getDeliveryCodes());

				if (!CollectionUtils.isEmpty(sourceData.getBizType())) {

					sourceData.getBizType().remove("-1");

				}

				delivery.setBizType(sourceData.getBizType());
			}

			order.setDelivery(delivery);

			Object delivered = orderDetail.get("delivered");

			order.setDelivered(false);

			if (delivered != null) {

				String[] statuses = delivered.toString().split(",");
				if (statuses.length == 3) {
					// 已发货或已收货 null代表发货时间为空
					if (!statuses[0].equals("NULL")) {
						// 非拼邮 或 拼邮已确认收货
						if (!statuses[1].equals("7") || statuses[2].equals("1")) {
							order.setDelivered(true);
							;
						}
					}
				}
			}

		} catch (Exception e) {

			e.printStackTrace();
		}

		// Refund
		if (!CollectionUtils.isEmpty(sourceData.getRefunds())) {

			List<Refund> refundList = new ArrayList<Refund>();

			sourceData.getRefunds().forEach(
					item -> {

						try {
							refundList.add((Refund) Utils.mapToObject(item,
									Refund.class));
						} catch (Exception e) {

							e.printStackTrace();
						}
					}
			);
			
			order.setRefunds(refundList);
		}

		// productProxy
		if (!CollectionUtils.isEmpty(sourceData.getProductProxies())) {

			List<ProductProxyPo> productProxies = new ArrayList<ProductProxyPo>();

			sourceData.getProductProxies().forEach(
					item -> {

						try {
							productProxies.add((ProductProxyPo) Utils
									.mapToObject(item, ProductProxyPo.class));
						} catch (Exception e) {

							e.printStackTrace();
						}
					});

			order.setProductProxies(productProxies);
		}

		// SubProduct
		if (!CollectionUtils.isEmpty(sourceData.getSubProducts())) {
			List<SubProduct> subProductList = new ArrayList<SubProduct>();

			sourceData.getSubProducts().forEach(
					item -> {

						try {
							subProductList.add((SubProduct) Utils.mapToObject(
									item, SubProduct.class));
						} catch (Exception e) {

							e.printStackTrace();
						}

					}
			);

			order.setSubProducts(subProductList);
		}

		// PrepaidCardRefund
		if (!CollectionUtils.isEmpty(sourceData.getPrepaidCardRefund())) {

			List<PrepaidCardRefund> prepaidCardRefundList = new ArrayList<PrepaidCardRefund>();

			sourceData
					.getPrepaidCardRefund()
					.forEach(
							item -> {

								try {
									prepaidCardRefundList.add((PrepaidCardRefund) Utils
											.mapToObject(item,
													PrepaidCardRefund.class));
								} catch (Exception e) {

									e.printStackTrace();
								}
							}
					);
			order.setPrepaidCardRefunds(prepaidCardRefundList);
		}
		
		// SubProduct
		if (!CollectionUtils.isEmpty(sourceData.getSubCodes())) {
			List<ProductSubCode> subCodeList = new ArrayList<ProductSubCode>();

			sourceData.getSubCodes().forEach(
					item -> {

						try {
							subCodeList.add((ProductSubCode) Utils.mapToObject(
									item, ProductSubCode.class));
						} catch (Exception e) {

							e.printStackTrace();
						}
					}
			);

			order.setProductSubCodes(subCodeList);
		}

		// prepaidCardNo
		order.setPrepaidCardNos(sourceData.getPrepaidCardNo());
		
		order.setPostDiscount(BigDecimal.ZERO);
		
		// pickupSotre
		if (!CollectionUtils.isEmpty(sourceData.getPickup())) {
			PickupStore pickupInStores = new PickupStore();
			
			try {
				pickupInStores = (PickupStore) Utils.mapToObject(sourceData
						.getPickup().get(0), PickupStore.class);
			} catch (Exception e) {

				e.printStackTrace();
			}

			order.setPickupStore(pickupInStores);
		}
		
		if (Utils.zeroIfNull(order.getGoodsOwnerId()) == 0) {

			order.setProxyOrderId(null);
			order.setPlaceRisk(null);
			
			// 二次确认会同步Logistic、authenticateFlag
			if (Utils.falseIfNull(order.getSecondConfirmTime() != null)) {

				order.getProducts().stream().forEach(product -> {
					product.setWaitInStock(null);
				});
				order.setGoodsOwnerId(null);
				// 取消订单会同步Logistic
			} else if (order.getTradingStatus() != 12)
				order.setLogistic(order.getLogistic() == false ? null : order
						.getLogistic());
			
		} else {
			Long proxyOrderId = this
					.selectMianOrderbyId(order.getMainOrderId())
					.getProxyOrderId();

			order.setProxyOrderId(proxyOrderId);
			order.setAuthenticateFlag(null);
			order.setOfficialLogistics(false);
		}

		return order;
	}
	
	public boolean isIncludeRebateProduct(long orderId) {
		
		int count = 0;
		
		List<OrderDetailPo> orderDetails = selectOrderDetailbyOrderId(orderId);
		
		for (OrderDetailPo detailPo : orderDetails) {
			
			if (Utils.zeroIfNull(detailPo.getSellerRebateRatio()).compareTo(BigDecimal.ZERO) == 0)
				count = count + 1;
		}
		
		if (count == orderDetails.size())
			return false;
		else
			return true;
	}
	
	public boolean isCancleOrder(OrderPo orderPo) {
		
		if (orderPo.getOrderStatus() == 12 || orderPo.getOrderStatus() == 18 || orderPo.getOrderStatus() == 13)
			return true;
		else
			return false;
	}

	public static void main(String args[]) throws Exception {

		/*
		 * List<OrderPo> OrderPo = new OrderShardingWapper()
		 * .selectHunterGiftBagOrderbyUserId(20238699);
		 */

		/*
		 * List<OrderDetailSubPo> subPoList = new OrderShardingWapper()
		 * .selectOrderDetailSubByDetailId(1041448699000L);
		 */

		// CommandShardingQuery commandShardingMapper;
		// commandShardingMapper = shardingSqlSessionw
		// .getMapper(CommandShardingQuery.class);
		// Logger.info(commandShardingMapper.getOrder(3298658699L));
		// Logger.info(commandShardingMapper.getOrder(3298658699L).get(0).get("order_id"));
		// Logger.info(new
		// OrderShardingWapper().selectCommandByOrder(3298658699L, 20238699));
		
//		System.out.println(new OrderShardingWapper()
//				.selectRefundBillListByOrderIdAndCatalogId(11366538699L,
//						"c7595925").size());
		
//		System.out.println(new OrderShardingWapper()
//				.selectOrderTravelersbyOrderId(11366538699L).get(0)
//				.getSellerId());
		
//		UserRefundFundDetailPo record = new UserRefundFundDetailPo();
//
//		Calendar calendar = Calendar.getInstance();
//		calendar.setTime(new Date());
//		calendar.add(Calendar.MINUTE, -6);
//
//		record.setPaymentId(15177328699L);
//		record.setUpdateTime(calendar.getTime());
//		record.setBizType(2);
//
//		new OrderShardingWapper().userRefundFundDetailMapper
//				.updateByPrimaryKeySelective(record);
		
//		System.out.println(new OrderShardingWapper()
//				.selectSellerProfitbyOrderId(6532998699L).get(0).getProfit());
		
//		System.out.println(new OrderShardingWapper()
//				.selectOrderExtbyOrderId(1));
		
//		OrderPo record = new OrderPo();
//
//		Calendar calendar = Calendar.getInstance();
//		calendar.setTime(new Date());
//		calendar.add(Calendar.DAY_OF_MONTH, -16);
//
//		record.setOrderId(3951808699L);
//		record.getShenqiLogisticsType();
//		record.setReceiveTime(calendar.getTime());
		
//		AutoReceiveOptPo record = new AutoReceiveOptPo();
//		
//		record.setOrderId(5623998699L);
//		record.setExpectReceiveTime(DateTime.now().plusDays(-1).toDate());
//		record.setFreeze(true);
//		record.setFreezeTime(DateTime.now().toDate());
//		record.setFinish(true);
//		
//		System.out.println(new OrderShardingWapper().autoReceiveOptMapper
//				.updateByPrimaryKey(record));
		// Logger.info(new
		// OrderShardingWapper().selectOrderDetailbyOrderId(3298658699L).get(0).getProxyPrice());
		
//		OrderPo record = new OrderShardingWapper().selectOrderbyOrderId(13312518699L);
//
//		Calendar calendar = Calendar.getInstance();
//		calendar.setTime(new Date());
//		calendar.add(Calendar.HOUR, -2);
//
//		record.setAddTime(calendar.getTime());
//
//		new OrderShardingWapper().orderMapper.updateByPrimaryKeySelective(record);
		
//		System.out.println(new OrderShardingWapper().selectOrderDetailbyOrderId(
//				1482360783L).size());
		
		System.out.println(new OrderShardingWapper()
				.selectPickInStorebyOrderId(14025848699L));
	}
}
